

#include <iostream>
#include <windows.h>
#include <conio.h>
#include "PlayGame.h"

void GameInterface();//开始界面
void ChooseLevel();//选择关卡
void StartGame(Player* player, bool* flag);//开始游戏
void EditorGame(Player* player, bool* flag);//编辑游戏
void ExitGame();//退出游戏


int main()
{
	Player* player;
	for (int i = 0; i < NumberOfCheckpoints; i++)
	{
		player = new Player[NumberOfCheckpoints];
	}

	//Player player[NumberOfCheckpoints];

	char myChoice;
	char yes_no;

	bool flag[NumberOfCheckpoints];
	for (int i = 0; i < NumberOfCheckpoints; i++)
	{
		flag[i] = false;//默认每一关都闯关失败
	}

	
	do
	{
		GameInterface();

		cin >> myChoice;
		system("cls");

		switch (myChoice)
		{
		case '1':
			StartGame(player, flag);//开始游戏
			break;
		case '2':
			EditorGame(player, flag);//编辑游戏
			break;	
		
		case '3':
			ExitGame();//退出游戏
			break;
		default:
			cout << "│────★输入非法！★────│" << endl;
			Sleep(1000);
			break;
		}

		if (myChoice == '0')
		{
			break;
		}

		do
		{
			system("cls");
			cout << "│────★是否回到主菜单？(Y/N)★────│" << endl;
			cin >> yes_no;
			if (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n')
			{
				system("cls");
				cout << "│────★输入非法！请重新输入！★────│" << endl;
				Sleep(1000);
			}
		} while (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n');

		if (yes_no == 'N' || yes_no == 'n')
		{
			ExitGame();
			break;
		}

	} while (yes_no == 'Y' || yes_no == 'y');

	delete[] player;
	return 0;
}

void GameInterface()//开始界面
{
	system("cls");
	system("@color 09");//淡蓝色前景色
	cout << "┌──────────────────────────────────────────────┐" << endl;
	cout << "│                                              │" << endl;
	cout << "│                 1.开始游戏                   │" << endl;
	cout << "│                                              │" << endl;
	cout << "│                 2.编辑游戏                   │" << endl;
	cout << "│                                              │" << endl;
	cout << "│                 3.退出游戏                   │" << endl;
	cout << "│                                              │" << endl;
	cout << "└──────────────────────────────────────────────┘" << endl;
}

void ChooseLevel()//选择关卡
{
	system("cls");
	system("@color 09");//淡蓝色前景色
	cout << "┌────★请选择关卡★────┐" << endl;
	for (int i = 0; i < NumberOfCheckpoints; i++)
	{
		cout << "│       " << i + 1 << "." << "第" << i + 1 << "关        │" << endl;
	}
	cout << "└──────────────────────┘" << endl;
}

void StartGame(Player* player, bool* flag)//开始游戏
{
	int i = 0;
	//bool flag = false;//每一关是否闯关成功
	char yes_no;

	system("@color 09");
	cout << "┌──────────────────────★游戏说明★───────────────────────┐" << endl;
	cout << "│  使用键盘方向键移动老鼠(鼠)，在规定的时间内走到粮仓(★) │" << endl;
	cout << "└─────────────────────────────────────────────────────────┘" << endl;
	Sleep(1500);



	do
	{
		system("cls");
		cout << "│────★第" << i + 1 << "关★────│" << endl;
		Sleep(1000);
		system("cls");

		player[i].ShowMap();
		flag[i] = player[i].Move();

		if (flag[i] == true)//若当前关卡闯关成功
		{
			system("cls");
			player[i].SaveMap();//保存并展示老鼠的路径
			system("pause");
			if (i == NumberOfCheckpoints - 1)
			{
				cout << "│────★您已通关所有关卡！祝贺您！★────│" << endl;
				system("pause");
				break;
			}

			do
			{
				cout << "┌────★路径生成完毕，请进行你的选择★────┐" << endl;
				cout << "│             ★Y.继续游戏★             │" << endl;
				cout << "│             ★N.结束游戏★             │" << endl;
				cout << "└────────────────────────────────────────┘" << endl;

				cin >> yes_no;
				if (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n')
				{
					cout << "│────★输入非法！请重新输入！★────│" << endl;
					Sleep(1000);
				}
			} while (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n');

			if (yes_no == 'Y' || yes_no == 'y')
			{
				i++;//地图下标+1，下一关
			}
			else
			{
				break;
			}

		}

	} while (flag[i - 1] == true && i - 1 < NumberOfCheckpoints);//成功通关且在关卡最大数量限制内


}

void EditorGame(Player* player, bool* flag)//编辑游戏
{
	int myChoice;

	do
	{
		ChooseLevel();

		cin >> myChoice;

		if (myChoice < 1 || myChoice > NumberOfCheckpoints)
		{
			cout << "│────★输入非法！请重新输入！★────│" << endl;
			Sleep(1000);
		}
	} while (myChoice < 1 || myChoice > NumberOfCheckpoints);

	system("@color 0d");//淡紫色前景色
	player[myChoice - 1].EditorMap();//编辑相应的地图
	flag[myChoice - 1] = false;
	system("cls");

	Sleep(500);
}



void ExitGame()//退出游戏
{
	system("@color 0c");//淡红色前景色
	cout << "│────★游戏结束，感谢体验★────│" << endl;
	system("pause");
}



